﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;


namespace BusinessLayer.RegInfo
{
    public class MarketingBL
    {        
        /// <summary>
        /// Get List of Marketing with full infomation
        /// </summary>
        /// <returns></returns>
        public List<MarketingFullInfo> GetAllFullInfoMarketings()
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            var list = adapter.GetList<MarketingFullInfo>(Common.Constants.StoredProcedures.GetAllFullInfoMarketing, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        /// <summary>
        /// Create Marketing Discount
        /// </summary>
        /// <param name="discount"></param>
        /// <returns></returns>
        public int CreateMarketingDiscount(MarketingDiscount discount)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@Code", discount.Code);
                adapter.AddParameter("@Title", discount.Title);
                adapter.AddParameter("@MarketingCatID", discount.MarketingCatID);
                adapter.AddParameter("@FromDate", discount.FromDate);
                adapter.AddParameter("@ToDate", discount.ToDate);
                adapter.AddParameter("@Status", discount.Status);
                adapter.AddParameter("@SubscriptionID", discount.SubscriptionID);
                //adapter.AddParameter("@CreatedAt", discount.CreatedAt);
                adapter.AddParameter("@CreatedBy", discount.CreatedBy);
                adapter.AddParameter("@Discount", discount.Discount);
                adapter.AddParameter("@Contents", discount.Contents);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertMarketingDiscount, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                //Khong hieu tai sao thuc hien ok lai tra ra -1. Tam thoi fix la 1
                result = 1;
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int CreateMarketingCoupon(MarketingCoupon coupon)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@Code", coupon.Code);
                adapter.AddParameter("@Title", coupon.Title);
                adapter.AddParameter("@MarketingCatID", coupon.MarketingCatID);
                adapter.AddParameter("@FromDate", (object)coupon.FromDate);
                adapter.AddParameter("@ToDate", (object)coupon.ToDate);
                adapter.AddParameter("@Status", coupon.Status);
                adapter.AddParameter("@SubscriptionID", coupon.SubscriptionID);
                //adapter.AddParameter("@CreatedAt", (object)coupon.CreatedAt);
                adapter.AddParameter("@CreatedBy", coupon.CreatedBy);                
                adapter.AddParameter("@CouponCode", coupon.CouponCode);
                adapter.AddParameter("@Discount", coupon.CouponDiscount);
                adapter.AddParameter("@Contents", coupon.Contents);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertMarketingCoupon, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
               
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public int UpdateMarketingDiscount(MarketingDiscount discount)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@MarketingID", discount.MarketingID);
                adapter.AddParameter("@Code", discount.Code);
                adapter.AddParameter("@Title", discount.Title);
                adapter.AddParameter("@MarketingCatID", discount.MarketingCatID);
                adapter.AddParameter("@FromDate", discount.FromDate);
                adapter.AddParameter("@ToDate", discount.ToDate);
                adapter.AddParameter("@Status", discount.Status);
                adapter.AddParameter("@SubscriptionID", discount.SubscriptionID);

                adapter.AddParameter("@LastUpdatedBy", discount.LastUpdatedBy);
                adapter.AddParameter("@Discount", discount.Discount);
                adapter.AddParameter("@Contents", discount.Contents);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.UpdateMarketingDiscount, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int UpdateMarketingCoupon(MarketingCoupon coupon)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@MarketingID", coupon.MarketingID);
                adapter.AddParameter("@Code", coupon.Code);
                adapter.AddParameter("@Title", coupon.Title);
                adapter.AddParameter("@MarketingCatID", coupon.MarketingCatID);
                adapter.AddParameter("@FromDate", (object)coupon.FromDate);
                adapter.AddParameter("@ToDate", (object)coupon.ToDate);
                adapter.AddParameter("@Status", coupon.Status);
                adapter.AddParameter("@SubscriptionID", coupon.SubscriptionID);
                adapter.AddParameter("@LastUpdatedBy", coupon.LastUpdatedBy);
                adapter.AddParameter("@Discount", coupon.CouponDiscount);
                adapter.AddParameter("@CouponCode", coupon.CouponCode);
                adapter.AddParameter("@Contents", coupon.Contents);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.UpdateMarketingCoupon, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
               
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        /// <summary>
        /// Get full info of a marketing code
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public MarketingFullInfo GetFullInfoByCode(string code)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetFullInfoMarketingByCode) { CommandType = CommandType.StoredProcedure };
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Code", code);
            var list = adapter.GetList<MarketingFullInfo>(Common.Constants.StoredProcedures.GetFullInfoMarketingByCode, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public MarketingFullInfo GettFullInfoByMarketingID(int id)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetFullInfoMarketingByID) { CommandType = CommandType.StoredProcedure };
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@MarketingID", id);
            var list = adapter.GetList<MarketingFullInfo>(Common.Constants.StoredProcedures.GetFullInfoMarketingByID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public int DeleteMarketingByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0, ids.Length - 1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                    result = adapter.ExecuteNonQuery(String.Format("DELETE FROM Marketing WHERE MarketingID IN ({0});", ids), CommandType.Text, CustomConnectionState.CloseOnExit);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
        public MarketingExtend GetMarketingCurrent(int cateId, int subId)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@MarketingCatID", cateId);
            adapter.AddParameter("@SubscriptionID", subId);
            var marketing = adapter.GetList<MarketingExtend>("sp_Marketing_GetCurrent", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit).FirstOrDefault();
            return marketing;
        }
    }
}
